package com.supermarket_project.mapper;

import com.supermarket_project.pojo.CashGood;
import com.supermarket_project.pojo.FindGoodName;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CashMapper {

    //通过商品条码查找商品名称
    CashGood findGoodNameByBarCode(FindGoodName findGoodName);

    //根据id查询商品的数量
    @Select("SELECT goods_number FROM goods WHERE goods_id = #{goodId}")
    Integer judge(CashGood cashGood);

    //根据barCode 查询goods_id
    @Select("SELECT goods_id FROM goods WHERE barcode=#{barCode};")
    Integer findGoodId(FindGoodName findGoodName);

    //根据根据barCode 查询activity_id
    @Select({"SELECT g_activity_id activity_id FROM goods WHERE barcode=#{barCode};"})
    Integer findActivityId(FindGoodName findGoodName);

    //根据goods_id查询折扣
    @Select("SELECT description discount FROM activity LEFT JOIN activity_type ON activity.ac_activity_type_id=activity_type.activity_type_id WHERE activity.activity_id=#{id}")
    Double findDiscount(Integer id);

    //存数据进cashier表 存入后status为1  affirm默认为0--->没被卖出
    @Insert("INSERT INTO cashier (good_id,staff_id,SUM,TIME,serial_number) VALUES (#{goodId},#{staffId},#{quantity},#{time},#{serialNumber})")
    boolean save(CashGood cashGood);

    //根据流水号serialNumber  修改affirm  affirm  的1为被卖出
    @Update("UPDATE cashier SET affirm=1 WHERE serial_number=#{serialNumber}")
    boolean upDateAffirm(String serialNumber);

    @Select("SELECT good_id goodId,SUM quantity FROM cashier WHERE serial_number=#{serialNumber}")
    List<CashGood> queryGoodsBySerialNumber(String serialNumber);

    @Update("UPDATE goods SET goods_number=goods_number-${quantity} WHERE goods_id=#{goodId}")
    int sellGoods(Integer goodId, Integer quantity);

    //根据流水号查商品的id 和数量
}
